﻿// dll_use_dynamic.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
//静态调用dll
#include <iostream>
#include "dll_aip.h"

#include <windows.h>  

//接口文档 https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/

static const char* filename = "demo_dll.dll";

int main()
{
    std::cout << "Hello World!\n";


    /* 加载 DLL到当前的地址空间*/
    HMODULE  hanlde = LoadLibraryA(filename);
    if (!hanlde)
    {
        std::cout << "fail to load " << filename << " dll." << std::endl;
        return -1;
    }

    /* 获取dll函数的地址 */
    //拿到函数
    /* 写法1
    Proc_Motor_Init func_motor_init =  (Proc_Motor_Init)GetProcAddress(hanlde,"Motor_Init");
    Proc_Motor_Run func_motor_run = (Proc_Motor_Run)GetProcAddress(hanlde, "Motor_Run");
    Proc_Motor_Stop func_motor_stop = (Proc_Motor_Stop)GetProcAddress(hanlde, "Motor_Stop");
    Proc_Motor_DeInit func_motor_deinit = (Proc_Motor_DeInit)GetProcAddress(hanlde, "Motor_DeInit");
    */
    //写法2
    auto func_motor_init = (Proc_Motor_Init)GetProcAddress(hanlde, "Motor_Init");
    auto func_motor_run = (Proc_Motor_Run)GetProcAddress(hanlde, "Motor_Run");
    auto func_motor_stop = (Proc_Motor_Stop)GetProcAddress(hanlde, "Motor_Stop");
    auto func_motor_deinit = (Proc_Motor_DeInit)GetProcAddress(hanlde, "Motor_DeInit");


    /*调用dll函数，完成功能*/
    func_motor_init("motor1");

    func_motor_deinit("motor1");

    /* 释放dll */

    FreeLibrary(hanlde);
    return 0;
}
